package sctu.edu.java.year2016.team02.huangguojing.day20160605.Chapter12_3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * Created by Eoly on 2016/6/6.
 */
public class Login extends JFrame implements ActionListener{
    private String userName;
    private String keyWords;
    private JLabel user;
    private JLabel key;
    private JTextField name;
    private JPasswordField keys;
    private Button enter;

    public Login(String title){
        super(title);
        setSize(400, 200);
        setResizable(false);
        setLayout(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        user = new JLabel("Username:");
        key = new JLabel("Key:");
        user.setBounds(50,25,75,25);
        key.setBounds(50,75,75,25);
        name = new JTextField("");
        keys = new JPasswordField("");
        name.setBounds(125, 25, 200, 25);
        keys.setBounds(125,75,200,25);
        enter = new Button("Enter");
        enter.setBounds(250,100,75,25);
        enter.addActionListener(this);
        add(user);add(key);add(name);add(keys);add(enter);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==enter){
            String word;
            if(name.getText()==null||(word=new String(keys.getPassword()))==null)
                JOptionPane.showMessageDialog(this, "输入不能为空", "Error", JOptionPane.ERROR_MESSAGE);
            else {
                String n=new String("用户:"+name.getText()+"\t");
                char[] a=keys.getPassword();
                String k=new String("密码："+new String(a,0,a.length)+"\n");
                FileOperate out = new FileOperate("d:/dev\\abc.txt");
                out.outPut(n,true);
                out.outPut(k,true);
                JOptionPane.showMessageDialog(this, "录入成功", "提示", JOptionPane.PLAIN_MESSAGE);
                name.setText("");
                keys.setText("");
            }
        }
    }
}
